package com.example.byNight;

import java.io.IOException;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Editable;
import android.text.Html;
import android.view.View;
import android.view.ViewStub;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class BYNightProjectActivity extends Activity {

	final static String bynight = "10.17.2.99";
	private static final String METHOD_NAME = "cerca";
	private static final String SOAP_ACTION = null;
	private static final String NAMESPACE = "http://prj/";
	private static final String URL = "http://" + bynight
			+ ":8080/ByNightWS/ByNightWS?WSDL";

	int counter;
	String prova;
	Button cerca, esci, reset;
	TextView display;
	EditText testo;
	RadioButton evento, locale;
	Toast toast;
	EventiLocali evLoc = new EventiLocali();
	Spinner spinner;
	String[] paths;
	ArrayAdapter<String> adapter;
	static int cont = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.enableDefaults();
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		counter = 0;
		cerca = (Button) findViewById(R.id.bCerca);
		testo = (EditText) findViewById(R.id.textCerca);
		esci = (Button) findViewById(R.id.esci);
		evento = (RadioButton) findViewById(R.id.evento);
		locale = (RadioButton) findViewById(R.id.locale);
		reset = (Button) findViewById(R.id.reset);
		cerca.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Editable cosa = testo.getText();
				Object ogg = null;
				if (locale.isChecked() == true) {
					List<String[]> locali = null;
					try {
						ogg = soap(cosa.toString(), "locale");
					} catch (Exception e) {
						System.out.println(e.getMessage().toString());
					}

					if (ogg != null) {
						locali = evLoc.getLocali(ogg);
						String sl = "";
						int i, j;
						int k = 0;
						paths = new String[locali.size() + 1];
						paths[0] = "Seleziona...";
						for (j = 0; j < locali.size(); j++) {
							sl = locali.get(j)[0] + ";" + locali.get(j)[1]
									+ ";" + locali.get(j)[2] + ";"
									+ locali.get(j)[3] + ";" + locali.get(j)[4]
									+ ";" + locali.get(j)[5] + ";"
									+ locali.get(j)[6] + ";" + locali.get(j)[7]
									+ ";" + locali.get(j)[8] + ";"
									+ locali.get(j)[9] + ";";
							System.out.println("sl=" + sl);
							paths[k + 1] = sl;
							k++;
						}
						Intent intent = new Intent(getApplicationContext(),
								ContextMenu.class);
						intent.putExtra("paths", paths);
						startActivity(intent);
						// finish();
					} else {
						Toast toast = Toast.makeText(v.getContext(),
								"Nessun risultato per il locale cercato",
								Toast.LENGTH_SHORT);
						toast.show();
					}
				} else if (evento.isChecked() == true) {

					List<String[]> eventi = null;
					try {
						ogg = soap(cosa.toString(), "evento");
					} catch (Exception e) {
						System.out.println(e.getMessage().toString());
					}
					if (ogg != null) {
						eventi = evLoc.getEventi(ogg);
						String se = "";
						int i, j;
						int k = 0;
						paths = new String[eventi.size() + 1];
						paths[0] = "Seleziona...";
						for (j = 0; j < eventi.size(); j++) {

							se = eventi.get(j)[0] + ";" + eventi.get(j)[1]
									+ ";" + eventi.get(j)[2] + ";"
									+ eventi.get(j)[3] + ";" + eventi.get(j)[4]
									+ ";" + eventi.get(j)[5] + ";";
							paths[k + 1] = se;
							k++;
						}
						Intent intent = new Intent(getApplicationContext(),
								ContextMenu.class);
						intent.putExtra("paths", paths);
						startActivity(intent);
						// finish();
					} else {
						Toast toast = Toast.makeText(v.getContext(),
								"Nessun risultato per l' evento cercato",
								Toast.LENGTH_SHORT);
						toast.show();
					}
				} else {
					toast = Toast.makeText(
							v.getContext(),
							"Attenzione!scelgli un opzione tra locale ed evento",
							Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});

		esci.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Esci();
			}
		});

		reset.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				testo.setText("");

			}
		});
	}

	public void Esci() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {

								Intent intent = new Intent(
										getApplicationContext(), Splash.class);
								intent.putExtra("esci", "esci");
								startActivity(intent);
								finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();

	}

	// public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String
	// NAMESPACE, String URL) throws IOException, XmlPullParserException {
	public Object soap(String cerca, String table) throws IOException,
			XmlPullParserException {

		// set up request
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		// variable name, value. I got the variable name, from the wsdl file!
		request.addProperty("cerca", cerca);
		// variable name, value. I got the variable name, from the wsdl file!
		request.addProperty("table", table);
		// put all required data into a soap
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// envelope prepare request
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		// this is optional, use it if you don't want to use a packet sniffer to
		// check what the sent message was (httpTransport.requestDump)
		httpTransport.debug = true;
		try {
			httpTransport.call(SOAP_ACTION, envelope); // send request
		} catch (Exception e) {
			System.out.println(httpTransport.requestDump);
		}
		try {
			Object result = envelope.getResponse(); // get response
			System.out.println("result contiene " + result.toString());
			return result;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}